import { _decorator, Component, Label, Node } from 'cc';
import ViewBase from '../engine/ViewBase';
import { UI_Cache_Type, UI_Config } from '../engine/UI_Config';
import { BundleConfig } from '../config/BundleConfig';
import { EventManager } from '../engine/EventManager';
import { UIManager } from '../engine/UIManager';
const { ccclass, property } = _decorator;

@ccclass('ui/ViewCheck')
export class ViewCheck extends ViewBase {

    //提示类型 
    public static Dialog_Type = {
        All: 0, //所有按钮类型，包括取消，确定，关闭
        OK: 1, //只有 Ok
    }


    //ui 配置 
    public static uiConfig: UI_Config = new UI_Config("ViewCheck",
        UI_Cache_Type.always, true, BundleConfig.main);


    private tipsStr: string; //提示内容
    private dialogType: number = ViewCheck.Dialog_Type.All; //提示类型
    private callback: Function; //回调函数
    private oKStr: string = ""
    private cancelStr: string = ""


    public onInitChild(tipsStr: string, dialogType: number,
        callback: Function = null, oKStr: string = "确  定", cancelStr: string = "取 消") {
        this.tipsStr = tipsStr;
        this.dialogType = dialogType;
        this.callback = callback;
        this.oKStr = oKStr;
        this.cancelStr = cancelStr;
    }

    start(): void {
        super.start();
        let controlUI: Node = this.findChild(this.contentNode, "controlUI")
        let lbInfo: Label = this.getChild(this.contentNode, "lbInfo", Label);
        lbInfo.string = this.tipsStr;

        let btnOK: Node = this.findChild(controlUI, "btnOK");
        this.getChild(btnOK, "Label", Label).string = this.oKStr;
        EventManager.onAddEvent(btnOK, () => {
            UIManager.instance.closeView(this);
            this.callback && this.callback(true);
        })
        let btnCancel = this.findChild(controlUI, "btnCancel");
        this.getChild(btnCancel, "Label", Label).string = this.cancelStr;
        EventManager.onAddEvent(btnCancel, () => {
            UIManager.instance.closeView(this);
            this.callback && this.callback(false);
        })
        let btnClose: Node = this.findChild(this.contentNode, "btnClose");
        EventManager.onAddEvent(btnClose, () => {
            UIManager.instance.closeView(this);
            this.callback && this.callback(false);
        })

        if (this.dialogType == ViewCheck.Dialog_Type.All) {
            btnCancel.active = true
            btnClose.active = true
        } else if (this.dialogType == ViewCheck.Dialog_Type.OK) {
            btnCancel.active = false
            btnClose.active = false
        }
    }
}

